package cn.cxion.chat.app.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.lqr.optionitemview.OptionItemView;
import com.lxj.xpopup.XPopup;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.bean.AppointmentInfo;
import cn.cxion.chat.app.bean.AppointmentLocale;
import cn.cxion.chat.app.bean.AppointmentTimes;
import cn.cxion.chat.app.bean.MessageEvent;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.third.location.data.LocationData;
import cn.cxion.chat.app.third.location.ui.activity.MyLocationActivity;
import cn.cxion.chat.app.util.CheckUtil;
import cn.cxion.chat.app.util.IflyUtil;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.UiUtils;
import cn.cxion.chat.app.view.popupview.AppointmentLocalePopupView;
import cn.cxion.chat.app.view.popupview.AppointmentTimePopupView;
import cn.cxion.chat.kit.WfcBaseActivity;
import cn.wildfirechat.message.LocationMessageContent;

public class AppointmentActivity extends WfcBaseActivity {


    @Bind(R.id.img_save)
    ImageView imgSave;
    @Bind(R.id.tv_title_right)
    TextView tvTitleRight;
    @Bind(R.id.appbar)
    AppBarLayout appbar;
    @Bind(R.id.et_suggest)
    AutoCompleteTextView etSuggest;
    @Bind(R.id.img_recognize)
    ImageView imgRecognize;
    @Bind(R.id.op_appointmentcustomer)
    OptionItemView opAppointmentcustomer;
    @Bind(R.id.op_appointmentlawyer)
    OptionItemView opAppointmentlawyer;
    @Bind(R.id.op_appointmenttime)
    OptionItemView opAppointmenttime;
    @Bind(R.id.op_place)
    OptionItemView opPlace;
    @Bind(R.id.ll_appointmentlawyer)
    PercentLinearLayout llAppointmentlawyer;


    private int classType;
    private int requestCode;

    AppointmentInfo appointmentInfo;
    LocationData locationData;


    public static void show(Activity activity, int classType, int requestCode) {
        Intent intent = new Intent(activity, AppointmentActivity.class);
        intent.putExtra("classType", classType);
        activity.startActivityForResult(intent, requestCode);
    }

    public static Intent getIntents(Context context, int classType) {
        Intent intent = new Intent(context, AppointmentActivity.class);
        intent.putExtra("classType", classType);
        return intent;
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_appointment;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        ButterKnife.bind(this);
        Intent intent = getIntent();
        classType = intent.getIntExtra("classType", 2);
        requestCode = intent.getIntExtra("resultCode", 0);
        viewControl();
        if (appointmentInfo == null) {
            appointmentInfo = new AppointmentInfo();
        }
    }

    private void viewControl() {
        setTitle(Constant.getClassKeyMap().get(classType));
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText("发送");
    }


    private void showAppointTime() {
        AppointmentTimePopupView appointmentTimePopupView = new AppointmentTimePopupView(this, appointmentInfo.getAppointmentTims());
        appointmentTimePopupView.setListener(new AppointmentTimePopupView.onDateSelectListener() {
            @Override
            public void selected(AppointmentTimes appointmentTimes) {
                appointmentInfo.setAppointmentTims(appointmentTimes);
                opAppointmenttime.setRightText(appointmentTimes.getTimeText());
            }
        });
        new XPopup.Builder(this)
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(appointmentTimePopupView)
                .show();
    }

    private void showAppointLocale() {
        AppointmentLocalePopupView localePopupView = new AppointmentLocalePopupView(this,appointmentInfo.getAppointmentLocale());
        localePopupView.setLinstener(new AppointmentLocalePopupView.OnLinstener() {
            @Override
            public void ok(AppointmentLocale appointmentLocale) {
                appointmentInfo.setAppointmentLocale(appointmentLocale);
                appointmentInfo.getAppointmentLocale().setLocationData(locationData);
                UiUtils.setRightText(appointmentLocale.getLocaleName(),opPlace);
            }
        });
        new XPopup.Builder(this)
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(localePopupView)
                .show();
    }

    private void showLocation() {
        String[] permissions = new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!((WfcBaseActivity) this).checkPermission(permissions)) {
                this.requestPermissions(permissions, 100);
                return;
            }
        }
        showAppointLocale();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
             locationData = (LocationData) data.getSerializableExtra("location");
            LocationMessageContent locCont = new LocationMessageContent();
            locCont.setTitle(locationData.getPoi());
            locCont.getLocation().setLatitude(locationData.getLat());
            locCont.getLocation().setLongitude(locationData.getLng());
            locCont.setThumbnail(locationData.getThumbnail());
            EventBus.getDefault().post(new MessageEvent(0,locationData.getPoi()));
        }
    }

    @OnClick({R.id.img_save, R.id.tv_title_right, R.id.img_recognize, R.id.op_appointmentcustomer, R.id.op_appointmentlawyer, R.id.op_appointmenttime, R.id.op_place, R.id.ll_appointmentlawyer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_save:
                break;
            case R.id.tv_title_right:
                appointmentInfo.setContent(etSuggest.getText().toString());
                if(CheckUtil.checkEditText(this,"内容不能为空",etSuggest)) {
                    Intent intent = new Intent();
                    intent.putExtra("appointmentInfo", appointmentInfo);
                    setResult(100, intent);
                    finish();
                }
                break;
            case R.id.img_recognize:
                IflyUtil iflyUtil = new IflyUtil();
                iflyUtil.recognize(this, etSuggest);
                break;
            case R.id.op_appointmentcustomer:
                ConstantActivity.show(this,Constant.APPOINTMENTCUSTOMER);
                break;
            case R.id.op_appointmentlawyer:
                ConstantActivity.show(this,Constant.APPOINTMENTLAWYER);
                break;
            case R.id.op_appointmenttime:
                showAppointTime();
                break;
            case R.id.op_place:
                showLocation();
                break;
            case R.id.ll_appointmentlawyer:
                break;
        }
    }
}
